package com.cmbchina.service;

import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 支付案例
 */
public class PayService {

    @Autowired
    private BankService bankService;

    private static final Set<String> VALID_CURRENCY_SET = new HashSet<String>(){{
        add("CNY"); add("USD"); add("EUR"); add("JPY"); add("HKD");
    }};

    /**
     * 支付
     * @param amount：支付金额
     * @param currency：支付币种
     * @param recipientId：接收方ID
     */
    public void pay(BigDecimal amount, String currency, Long recipientId){
        //校验支付金额合法性且大于0
        if(Objects.isNull(amount) || amount.compareTo(BigDecimal.ZERO) <= 0){
            throw new IllegalArgumentException("amount must be positive");
        }

        //币种参数合法性校验
        if(Objects.isNull(currency)){
            throw new IllegalArgumentException("currency cannot be empty");
        }

        //校验币种系统支持的币种
        if(Objects.isNull(currency) || !VALID_CURRENCY_SET.contains(currency.toUpperCase())){
            throw new IllegalArgumentException("invalid currency or not support for now");
        }

        bankService.transfer(amount, currency, recipientId);
    }
}
